package com.zbec.mdrs.repository.jpa;

import com.zbec.mdrs.entity.drafts.Drafts;
import com.zbec.mdrs.vo.DraftsVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by fxl on 2015/10/17.
 */
public interface DraftsDao extends PagingAndSortingRepository<Drafts, Long>, JpaSpecificationExecutor<Drafts> {
    List<Drafts> findByHistoryId(Long historyId);

    List<Drafts> findByHistoryIdAndOrgId(Long historyId,Long orgId);

    @Query("select new com.zbec.mdrs.vo.DraftsVo(d.id,d.zzjgdm,d.sjsj,d.cprmc,d.jrjgbm,d.bz,d.hpje," +
            " d.hpcdr,d.hpdqr,d.hpfkrq,d.bzjbl,d.dbbz,d.wjfl,d.dkbz,d.cdxyh,d.source,d.recordStatus,d.reportStatus," +
            "d.existError,d.errorDesc,d.missedField,d.recordId) from Drafts d  " +
            "where (''= :sjsj or d.sjsj = :sjsj) "+
            "and (''= :recordStatus or d.recordStatus = :recordStatus) " +
            "and (''= :existError or d.existError = :existError)  ")
    Page<DraftsVo> findAllDrafts(@Param("sjsj") String sjsj, @Param("recordStatus") String recordStatus, @Param("existError") String existError, Pageable pageable);


  @Query("select new com.zbec.mdrs.vo.DraftsVo(d.id,d.zzjgdm,d.sjsj,d.cprmc,d.jrjgbm,d.bz,d.hpje," +
            " d.hpcdr,d.hpdqr,d.hpfkrq,d.bzjbl,d.dbbz,d.wjfl,d.dkbz,d.cdxyh,d.source,d.recordStatus,d.reportStatus," +
            "d.existError,d.errorDesc,d.missedField,d.recordId) from Drafts d  " +
            "where (''= :historyId or d.history.id = :historyId) ")
    Page<DraftsVo> findDraftsByHistroy(@Param("historyId")String historyId,Pageable pageable);


    @Query("select l from Drafts l  " +
            "where (''= :sjsj or l.sjsj = :sjsj) "+
            "and (''= :recordStatus or l.recordStatus = :recordStatus) " +
            "and (''= :existError or l.existError = :existError) order by l.zzjgdm  ")
    List<Drafts>  findDraftsForExport(@Param("sjsj")String sjsj,@Param("recordStatus")String recordStatus,@Param("existError")String existError);

    @Query("select l from Drafts l  " +
            "where l.sjsj = :sjsj  and l.recordStatus <>  :recordStatus  and  l.cdxyh = :cdxyh  ")
    List<Drafts> findUpdateDrafts(@Param("cdxyh")String cdxyh ,@Param("sjsj")String sjsj,@Param("recordStatus")String recordStatus);



    @Query( "select max(sjsj) from Drafts where sjsj < :sjsj")
    String getMaxDraftsFny(@Param("sjsj")String sjsj);


    @Query("select l from Drafts l where l.recordStatus !=3 and l.recordId = :recordId and l.sjsj = :sjsj ")
    Drafts getDraftsFromDrmap(@Param("recordId")Long recordId,@Param("sjsj")String sjsj);

    @Query("select l from Drafts l  " +
            "where l.sjsj = :sjsj  and l.recordStatus = :recordStatus  and  l.reportStatus <> :reportStatus " +
            "and (''= :jrjgbm or l.jrjgbm = :jrjgbm)  order by l.zzjgdm  ")
    List<Drafts> getActiveDraftsByYwMonth(@Param("sjsj")String sjsj ,@Param("recordStatus")String recordStatus,
                                          @Param("reportStatus")String reportStatus,@Param("jrjgbm")String jrjgbm);
}
